package com.xcm.sso;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcm.cache.RedisCacheDao;
import com.xcm.constant.CacheConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Map;

/**
 * redis定时任务（清理无用的数据）
 *
 * @author linqiang
 * @date 2018-06-12 9:14
 */
@Component
public class RedisQuartzService {

    @Autowired
    private RedisCacheDao redisCacheDao;
    @Autowired
    private SSOProperties ssoProperties;

    /**
     * 每天凌晨1点执行删除redis中无用用户信息和ticket
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void deleteUselessDataInRedis() {
        Map<Object, Object> allKeys = redisCacheDao.getAllCache(ssoProperties.getWholeCacheName());
        Map<Object, Object> allTicketKeys = redisCacheDao.getAllCache(ssoProperties.getSsoTicketsCacheName());
        Object value = null;
        JSONObject userInfo = null;
        for (Map.Entry<Object, Object> entry : allKeys.entrySet()) {
            value = entry.getValue();
            if (null != value) {
                try {
                    userInfo = JSONObject.parseObject(value.toString());
                } catch (Exception e) {
                    redisCacheDao.deleteByKey(ssoProperties.getWholeCacheName(), entry.getKey());
                }
                if (null != userInfo) {
                    long expire = userInfo.getLongValue(SSOContants.EXPIRY);
                    long now = System.currentTimeMillis();
                    if (now >= expire) {
                        redisCacheDao.deleteByKey(ssoProperties.getWholeCacheName(), entry.getKey());
                    }
                }
            } else {
                redisCacheDao.deleteByKey(ssoProperties.getWholeCacheName(), entry.getKey());
            }
        }
        Ticket ticket = null;
        JSONObject ticketJson = null;
        for (Map.Entry<Object, Object> ticketEntry : allTicketKeys.entrySet()) {
            value = ticketEntry.getValue();
            if (null != value) {
                try {
                    ticketJson = JSONObject.parseObject(value.toString());
                } catch (Exception e) {
                    redisCacheDao.deleteByKey(ssoProperties.getSsoTicketsCacheName(), ticketEntry.getKey());
                }
                if (null != ticketJson) {
                    ticket = JSON.toJavaObject(ticketJson, Ticket.class);
                    if (null != ticket) {
                        Timestamp recoverTime = ticket.getRecoverTime();
                        long expire = recoverTime.getTime();
                        long now = System.currentTimeMillis();
                        if (now >= expire) {
                            redisCacheDao.deleteByKey(ssoProperties.getSsoTicketsCacheName(), ticketEntry.getKey());
                        }
                    } else {
                        redisCacheDao.deleteByKey(ssoProperties.getSsoTicketsCacheName(), ticketEntry.getKey());
                    }
                }
            }
        }
    }
}
